import { app, BrowserWindow, ipcMain, Menu } from "electron"
import path from 'path'
import { fileURLToPath } from 'url'



const getDirName = () => {
  if (process.env.ENV_NOW == 'dev') {
    const __filename = fileURLToPath(import.meta.url)
    const dirname = path.dirname(__filename)
    return dirname
  } else {
    return __dirname
  }
}



const log = (...args: any[]) => {
  console.log(`[${(new Date).toLocaleString()}]`, ...args)
}

log("electron start", process.env.ENV_NOW)

// app.setAsDefaultProtocolClient('electron')

let url: string
if (process.env.ENV_NOW == 'dev') {
  url = `http://localhost:${process.env.WEB_PORT}/`
} else if (process.env.ENV_NOW == 'prod') {
  url = `file://${path.join(getDirName(), 'index.html')}`
}


let mainWindow





app.on("ready", () => {
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(getDirName(), 'preload.js'),
    }
  });
  mainWindow.loadURL(url)
  mainWindow.webContents.openDevTools()
  log(`electron load ${url}`)



  ///////////////////////////////////

  ipcMain.handle('system:exit', () => {
    app.quit()
  })

  let menu = Menu.buildFromTemplate([
    {
      label: '文件',
      submenu: [
        {
          label: '新建',
          click() {
            console.log('new file');
          }
        },
        {
          label: '打开',
          click() {
            console.log('open folder');
          }
        },
        {
          label: '导入到画布',
        },
        {
          label: '最近使用的文件',
          role: 'recentDocuments',
        },
        {
          label: '保存',
        },
        {
          label: '另存为',
          submenu: [
            {
              label: 'PNG图片',
            },
            {
              label: 'JPG图片',
            },

            {
              label: 'BMP图片'
            },
            {
              label: 'GIF图片'
            },
            {
              label: '其他格式'
            },
          ]
        },
        {
          type: 'separator'
        },
        {
          label: '最小化',
          role: 'minimize',
        },
        {
          label: '关于',
          role: 'about'
        },
      ]
    },
    {
      label: '编辑',
      toolTip: '常规编辑',
      submenu: [
        {
          label: '复制',
          role: 'copy'
        },
        {
          label: '剪切',
          role: 'cut'
        },
        {
          label: '粘贴',
          role: 'paste'
        }
      ]
    },
    {
      label: '查看',
      submenu: [
        {
          label: '缩放',
          role: 'zoom',
          submenu: [
            {
              label: '放大',
              role: 'zoomIn',
            },
            {
              label: '缩小',
              role: 'zoomOut',
            },
            {
              label: '重置大小',
              role: 'resetZoom',
            },
            {
              label: '适应窗口',
            }
          ]
        },
        {
          type: 'separator',
        },
        {
          label: '标尺',
          accelerator: 'Ctrl + R'
        },
        {
          label: '网格',
          accelerator: 'Ctrl + G',
          click() {
            console.log('toggle grid');
          }
        },
        {
          label: '状态栏',
        },
        {
          type: 'separator',
        },
        {
          label: '刷新',
          role: 'reload',
        },
        {
          label: '全屏',
          role: 'togglefullscreen',
          accelerator: 'F11',
        }
      ]
    },
  ]
  )

  Menu.setApplicationMenu(menu)


})
